
<Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> Friend Class TableHolder

    Private m_name As String = ""
    Private m_description As BikDescription = Nothing
    Private m_firstNode As TableCont = Nothing

    Friend Sub New(ByVal Name As String)
        m_name = Trim(Name)
    End Sub
    Friend Function addNode(ByRef map_ As TableCont) As Boolean
        If Not map_.Name = vbNullString Then
            If IsNothing(m_firstNode) Then
                m_firstNode = map_
            Else
                m_firstNode.addNode(map_)
            End If
            Return True
        Else
            Return False
        End If
    End Function

    Public Property Name() As String
        Get
            Return m_name
        End Get
        Set(ByVal value As String)
            m_name = value
        End Set
    End Property
    Public Property Description() As BikDescription
        Get
            Return m_description
        End Get
        Set(ByVal value As BikDescription)
            m_description = value
        End Set
    End Property

    Friend Function length() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstNode) Then
            intReturn = m_firstNode.length()
        End If
        Return (intReturn)
    End Function
    Friend Function lengthTotal() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstNode) Then
            intReturn = m_firstNode.lengthTotal()
        End If
        Return (intReturn)
    End Function
    Friend Function count() As Integer
        Dim intReturn As Integer = 1
        If Not IsNothing(m_firstNode) Then
            intReturn += m_firstNode.count
        End If
        Return intReturn
    End Function
    Friend Function countTotal() As Integer
        Dim intReturn As Integer = 0
        If Not IsNothing(m_firstNode) Then
            intReturn = m_firstNode.countTotal
        End If
        Return intReturn
    End Function

    Friend Function getTableByAddress(ByVal Address As Integer) As Table
        Dim f_tables As TableCont() = Me.getAsArray
        Dim f_tab As Table() = Nothing
        Dim f_found As Table = Nothing
        If Not IsNothing(m_firstNode) Then
            For Each tableCon As TableCont In f_tables
                If IsNothing(f_found) Then
                    If Not IsNothing(tableCon.getFirstTable) Then
                        For Each tab As Table In tableCon.getAsArray
                            If tab.Address = Address Then
                                f_found = tab
                                Exit For
                            End If
                        Next
                    End If
                Else
                    Exit For
                End If
            Next
        End If
        Return f_found
    End Function
    Friend Function getTableByName(ByVal Name As String) As Table
        Dim f_tables As TableCont() = Me.getAsArray
        Dim f_tab As Table() = Nothing
        Dim f_found As Table = Nothing
        If Not IsNothing(m_firstNode) Then
            For Each tableCon As TableCont In f_tables
                If IsNothing(f_found) Then
                    If Not IsNothing(tableCon.getFirstTable) Then
                        For Each tab As Table In tableCon.getAsArray
                            If LCase(Trim(tab.Name)) = LCase(Trim(Name)) Then
                                f_found = tab
                                Exit For
                            End If
                        Next
                    End If
                Else
                    Exit For
                End If
            Next
        End If
        Return f_found
    End Function
    Friend Function getTablesAsArray() As Table()
        If Not IsNothing(m_firstNode) Then
            Dim f_return(m_firstNode.countTotal - 1) As Table
            Dim f_count As Integer = 0
            For Each tabCon As TableCont In Me.getAsArray
                If tabCon.countTables >= 1 Then
                    For Each tab As Table In tabCon.getAsArray
                        If Not IsNothing(tab) Then
                            f_return(f_count) = tab
                            f_count += 1
                        End If
                    Next
                End If
            Next
            ReDim Preserve f_return(f_count - 1)
            Return f_return
        Else
            Return Nothing
        End If
    End Function
    Friend Function getByName(ByVal Name As String) As TableCont
        Dim f_cursor As TableCont
        Dim found As Boolean = False
        Name = Trim(Name)
        If Not IsNothing(m_firstNode) Then
            f_cursor = m_firstNode
            If Trim(f_cursor.Name) = Name Then
                found = True
                GoTo endMod
            End If
            While Not found
                If Not IsNothing(f_cursor.getNext) Then
                    f_cursor = f_cursor.getNext
                    If Trim(f_cursor.Name) = Name Then
                        found = True
                        GoTo endMod
                    End If
                Else
                    found = False
                    GoTo endMod
                End If
            End While
        Else
            Return Nothing
        End If
endMod:
        If found Then Return f_cursor Else Return Nothing
    End Function
    Friend Function getByIndex(ByVal Index_ As Integer) As TableCont
        Dim f_cursor As TableCont
        Dim found As Boolean = False
        If Not IsNothing(m_firstNode) Then
            f_cursor = m_firstNode
            If Index_ = 0 Then
                Return f_cursor
                found = True
            Else
                For count As Integer = 1 To Index_
                    If Not IsNothing(f_cursor.getNext) Then
                        f_cursor = f_cursor.getNext
                        found = True
                    Else
                        Return Nothing
                        Exit For
                    End If
                Next
            End If
        Else
            Return Nothing
        End If
        If found = False Then Return Nothing Else Return f_cursor
    End Function
    Friend Function getAsArray() As TableCont()
        Dim f_count As Integer
        Dim f_mapArray() As TableCont = Nothing
        If Not IsNothing(m_firstNode) Then
            f_count = m_firstNode.count
            ReDim f_mapArray(0 To f_count - 1)
            For i As Integer = 0 To f_count - 1
                f_mapArray(i) = Me.getByIndex(i)
            Next
        End If
        Return f_mapArray
    End Function
    Friend Function removeByName(ByVal Name As String) As Boolean
        Dim s_first As TableCont
        Dim s_temp As TableCont
        Dim found As Boolean = False

        If Not IsNothing(m_firstNode) Then
            If m_firstNode.Name = Name Then
                If IsNothing(m_firstNode.getNext) Then
                    m_firstNode = Nothing
                Else
                    s_temp = m_firstNode.getNext
                    m_firstNode = Nothing
                    Me.addNode(s_temp)
                End If
            Else
                ' First node not a match.. now check all other nodes
                s_first = m_firstNode
                Do
                    If s_first.getNext.Name = Name Then
                        'next node is a match..
                        If IsNothing(s_first.getNext.getNext) Then
                            'next node has no next.. so just null delete the link
                            s_first.setNext(Nothing)
                            found = True
                            GoTo endSub
                        Else
                            'the next node has a next.. so must swap the links
                            s_temp = s_first.getNext.getNext
                            s_first.setNext(s_temp)
                            found = True
                            GoTo endSub
                        End If
                    End If
                    s_first = s_first.getNext
                Loop Until found Or IsNothing(s_first.getNext)
            End If
        End If
endSub:
        s_first = Nothing
        s_temp = Nothing
        If found Then Return True Else Return False
    End Function
    Friend Function removeByIndex(ByVal Index_ As Integer) As Boolean
        Dim ret As Boolean = False
        Dim f_map As TableCont = Me.getByIndex(Index_)
        If Not IsNothing(f_map) Then
            Me.removeByName(f_map.Name)
            ret = True
        End If
        Return ret
    End Function
    Friend Function getFirst() As TableCont
        Return m_firstNode
    End Function

    Friend Sub InterpolateNewScale(ByVal iArray() As Byte, ByVal iScale As Scale, ByVal iScaleArray() As Single)

    End Sub
End Class
